﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DanhmucThietbi;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace DanhmucThietbi
{
    public partial class frmThietBi : BasicForm.parent2
    {
        enum ActionMode
        {
            NEW = 0,
            EDIT = 1,
            DEL = 2
        }
        ActionMode AcMode = ActionMode.NEW;
        ThietBiInfo ObjThietBi = null;
        CtrThietBi Ctr = new CtrThietBi();
        DataTable DSLinkKien = new DataTable();
        public frmThietBi()
        {
            InitializeComponent();
            cbxPLoaiLK.Properties.DataSource = (new CtrPhanLoai()).GetAll();
            cbxNhaCCLK.Properties.DataSource = (new CtrNhaCC()).GetAll();
            cbxPLoai.Properties.DataSource = (new CtrPhanLoai()).GetAll();
            cbxNhaCC.Properties.DataSource = (new CtrNhaCC()).GetAll();
        }

        private void frmThietBi_Load(object sender, EventArgs e)
        {
            DSLinkKien = Ctr.GetLKByTBID("000");
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            //xoa form.
            ClearFormTB();
            txtMaTB.Properties.ReadOnly = false;
            txtMaTB.Focus();
            ObjThietBi = new ThietBiInfo();
            AcMode = ActionMode.NEW;
            DSLinkKien = Ctr.GetLKByTBID("000");
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            DataRow NewItem = DSLinkKien.NewRow();
            NewItem["MA_LIENKET"] = txtMaTB.Text;
            NewItem["MA_TB"] = txtMaLK.Text;
            NewItem["TEN_TB"] = txtTenLK.Text;
            NewItem["MA_PLOAI"] = cbxPLoaiLK.Text;
            NewItem["MA_NHACC"] = cbxNhaCCLK.Text;
            NewItem["THONGTIN_TB"] = txtThongtinLK.Text;
            NewItem["NGAY_SX"] = dtpNgaySXLK.Text;
            NewItem["NGAY_MUA"] = dtpNgayMuaLK.Text;
            NewItem["NGAY_SDUNG"] = dtpNgaySDLK.Text;
            NewItem["NGUYEN_GIA"] = txtNguyenGiaLK.Text;
            NewItem["THOIGIAN_BHANH"] = txtThoiGianBHLK.Text;
            DSLinkKien.Rows.Add(NewItem);
            this.gridControl1.DataSource = DSLinkKien;
        }

        public void InitGrid()
        {
            // gridView1
            // 
            GridColumn[] Cols = new GridColumn[10];
            // 
            // gridColumn1
            // 
            int i = 0;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Mã Linh Kiện";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 0;
            Cols[i].FieldName = "MA_TB";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Caption = "Tên Linh Kiện";
            Cols[i].Width = 200;
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "TEN_TB";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Phân Loại";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "MA_PLOAI";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Sản Xuất";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_SX";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Nhà Cung Cấp";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "MA_NHACC";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Mua";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_MUA";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Ngày Sử Dụng";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGAY_SDUNG";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Nguyên Giá";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "NGUYEN_GIA";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Thời Gian Bảo Hành";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "THOIGIAN_BHANH";

            i++;
            Cols[i] = new GridColumn();
            Cols[i].Width = 100;
            Cols[i].Caption = "Thông Tin Linh Kiện";
            Cols[i].Name = "Cols" + i.ToString();
            Cols[i].Visible = true;
            Cols[i].VisibleIndex = 1;
            Cols[i].FieldName = "THONGTIN_TB";

            //GridColumn gridColumn2 = new GridColumn();
            this.gridView1.Columns.AddRange(Cols);
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            this.gridView1.IndicatorWidth = 30;
            //this.gridView1.OptionsSelection.
            //this.gridView1.
            this.gridControl1.DataSource = Ctr.GetLKByTBID(txtMaTB.Text);
        }

        public void ClearFormTB()
        {
            txtMaTB.Properties.ReadOnly = false;
            txtMaTB.Text = string.Empty;
            txtTenTB.Text = string.Empty;
            cbxPLoai.Text = string.Empty;
            dtpNgaySX.Text = string.Empty;
            cbxNhaCC.Text = string.Empty;
            dtpNgayMua.Text = string.Empty;
            dtpNgaySDung.Text = string.Empty;
            txtGia.Text = string.Empty;
            txtBHanh.Text = string.Empty;
            txtThongTin.Text = string.Empty;
            txtMaTB.Focus();

        }

        public void ClearFormLK()
        {
            txtMaLK.Properties.ReadOnly = false;
            txtMaLK.Text = string.Empty;
            txtTenLK.Text = string.Empty;
            cbxPLoaiLK.Text = string.Empty;
            dtpNgaySXLK.Text = string.Empty;
            cbxNhaCCLK.Text = string.Empty;
            dtpNgayMuaLK.Text = string.Empty;
            dtpNgaySDLK.Text = string.Empty;
            txtNguyenGiaLK.Text = string.Empty;
            txtThoiGianBHLK.Text = string.Empty;
            txtThongtinLK.Text = string.Empty;
            txtMaLK.Focus();

        }

        public bool CheckDate()
        {
            if ((dtpNgaySX.DateTime > dtpNgayMua.DateTime) || (dtpNgaySX.DateTime > dtpNgaySDung.DateTime))
            {
                MessageBox.Show("Ngày sản xuất lớn hơn ngày mua hoặc ngày sử dụng.");
                dtpNgaySX.Text = string.Empty;
                return false;

            }
            if ((dtpNgayMua.DateTime > dtpNgaySDung.DateTime))
            {
                MessageBox.Show("Ngày mua lớn hơn ngày sử dụng.");
                dtpNgayMua.Text = string.Empty;
                return false;
            }
            return true;
        }


        private void simpleButton4_Click(object sender, EventArgs e)
        {
            ObjThietBi = new ThietBiInfo();
            ObjThietBi.MA_TB = txtMaTB.Text;
            if (txtMaTB.Text == null || txtMaTB.Text == string.Empty)
            {
                MessageBox.Show("Bạn chưa nhập mã Thiết Bị.");
                return;
            }
            else
            {
                ObjThietBi.MA_TB = txtMaTB.Text;
            }
            ObjThietBi.TEN_TB = txtTenTB.Text;
            ObjThietBi.MA_PLOAI = cbxPLoai.EditValue.ToString();
            if (dtpNgaySX.Text == null || dtpNgaySX.Text == string.Empty)
            {
                ObjThietBi.NGAY_SX = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_SX = DateTime.Parse(dtpNgaySX.Text);
            }
            ObjThietBi.MA_NHACC = cbxNhaCC.EditValue.ToString();

            if (dtpNgayMua.Text == null || dtpNgayMua.Text == string.Empty)
            {
                ObjThietBi.NGAY_MUA = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_MUA = DateTime.Parse(dtpNgayMua.Text);
            }

            if (dtpNgaySDung.Text == null || dtpNgayMua.Text == string.Empty)
            {
                ObjThietBi.NGAY_SDUNG = DateTime.Now;
            }
            else
            {
                ObjThietBi.NGAY_SDUNG = DateTime.Parse(dtpNgaySDung.Text);
            }
            if (txtGia.Text == null || txtGia.Text == string.Empty)
            {
                ObjThietBi.NGUYEN_GIA = 0;
            }
            else
            {
                ObjThietBi.NGUYEN_GIA = int.Parse(txtGia.Text);
            }
            if (txtBHanh.Text == null || txtBHanh.Text == string.Empty)
            {
                ObjThietBi.THOIGIAN_BHANH = 0;
            }
            else
            {
                ObjThietBi.THOIGIAN_BHANH = int.Parse(txtGia.Text);
            }
            ObjThietBi.THONGTIN_TB = txtThongTin.Text;
            ObjThietBi.MA_LIENKET = null;
            ObjThietBi.IS_LKIEN = false;
            if (!CheckDate())
            {
                return;
            }
            if (AcMode == ActionMode.NEW) //them mới
            {
                try
                {
                    Ctr.InsertItem(ObjThietBi);
                    this.gridControl1.DataSource = Ctr.GetLKByTBID(txtMaTB.Text);
                    if (MessageBox.Show("Bạn muốn thêm tiếp không?", "Tiếp tục", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ClearFormTB();
                        ObjThietBi = new ThietBiInfo();
                    }
                    else
                    {
                        txtMaTB.Properties.ReadOnly = true;
                        AcMode = ActionMode.EDIT;
                    }
                }
                catch (Exception ex)
                {
                    txtMaTB.Focus();
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
            else
            {
                if (AcMode == ActionMode.EDIT) //chỉnh sửa
                {
                    try
                    {
                        Ctr.UpdateItem(ObjThietBi);
                        this.gridControl1.DataSource = Ctr.GetLKByTBID(txtMaTB.Text);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else //xóa
                {
                    try
                    {
                        Ctr.DeleteItem(ObjThietBi);
                        this.gridControl1.DataSource = Ctr.GetLKByTBID(txtMaTB.Text);
                    }
                    catch (Exception ex)
                    {
                        //MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void btnChonTB_Click(object sender, EventArgs e)
        {
            
            using (frmSubThietBi fsubTB = new frmSubThietBi())
            {
                if (fsubTB.ShowDialog() == DialogResult.OK)
                {
                    txtMaTB.Text = fsubTB.ObjSubThietBi.MA_TB;
                    txtTenTB.Text = fsubTB.ObjSubThietBi.TEN_TB;
                    cbxPLoai.Text = fsubTB.ObjSubThietBi.MA_PLOAI;
                    cbxNhaCC.Text = fsubTB.ObjSubThietBi.MA_NHACC;
                    txtThongTin.Text = fsubTB.ObjSubThietBi.THONGTIN_TB;
                    dtpNgaySX.Text = fsubTB.ObjSubThietBi.NGAY_SX.ToString();
                    dtpNgayMua.Text = fsubTB.ObjSubThietBi.NGAY_MUA.ToString();
                    dtpNgaySDung.Text = fsubTB.ObjSubThietBi.NGAY_SDUNG.ToString();
                    txtGia.Text = fsubTB.ObjSubThietBi.NGUYEN_GIA.ToString();
                    txtBHanh.Text = fsubTB.ObjSubThietBi.THOIGIAN_BHANH.ToString();
                }
            }
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            using (frmSubLinhKien fsubLK = new frmSubLinhKien())
            {
                if (fsubLK.ShowDialog() == DialogResult.OK)
                {
                    DSLinkKien.Rows.Add(fsubLK.DSSubLinhKien);
                    this.gridControl1.DataSource = DSLinkKien;
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            AcMode = ActionMode.DEL;
            simpleButton4.PerformClick();
            txtMaTB.Properties.ReadOnly = false;
        }

    }
}
